"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mediaRouter = exports.createConstraint = void 0;
const express_1 = __importDefault(require("express"));
const log4js_1 = require("log4js");
const MediaEntity_1 = require("../entity/MediaEntity");
const utils = __importStar(require("../api-helper"));
const logger = (0, log4js_1.getLogger)('route/media');
const createMediaConstraints = utils.create(MediaEntity_1.MediaConstraintEntity);
const updateMediaConstraints = utils.update(MediaEntity_1.MediaConstraintEntity);
function createConstraint(mediaConstraint = {}) {
    if (mediaConstraint) {
        const { video, audio } = mediaConstraint;
        if (!video)
            mediaConstraint.video = '0';
        else if (video === true) {
            mediaConstraint.video = '1';
        }
        if (!audio)
            mediaConstraint.audio = '0';
        else if (video === true) {
            mediaConstraint.video = '1';
        }
    }
    return mediaConstraint;
}
exports.createConstraint = createConstraint;
function mediaRouter(app) {
    const roomRouter = express_1.default.Router();
    roomRouter.post('/create', async ({ body }, res) => {
        logger.debug('Create media: ' + JSON.stringify(body, null, 2));
        let mediaConstraint = createConstraint(body);
        let dbMediaConstraint = new MediaEntity_1.MediaConstraintEntity();
        if (!body) {
            res.status(200).send({ result: 'Error', message: 'request body error!' });
            return;
        }
        try {
            if (body.id) {
                const dbMedia = await MediaEntity_1.MediaConstraintEntity.findOneBy({ id: body.id });
                if (dbMedia) {
                    res.status(200).send({
                        result: 'Error', data: dbMedia,
                        message: 'mediaConstraint already Created!'
                    });
                    return;
                }
            }
            dbMediaConstraint = Object.assign(dbMediaConstraint, mediaConstraint);
            dbMediaConstraint.createTime = Date.now().toString();
            dbMediaConstraint.lastActiveTime = Date.now().toString();
            dbMediaConstraint = await dbMediaConstraint.save();
        }
        catch (error) {
            console.error(error);
            res.status(200).send({ result: 'Error', message: error.message });
        }
        if (dbMediaConstraint) {
            res.status(200).send({ result: 'OK', data: dbMediaConstraint });
        }
        else {
            res.status(200).send({ result: 'Error', message: 'classroom already Created!' });
        }
    });
    roomRouter.post('/update', async (req, res) => {
        let dbUser;
        logger.debug('Update room: ' + JSON.stringify(req.body, null, 2));
        try {
            if (Array.isArray(req.body)) {
                dbUser = [];
                for (const member of req.body) {
                    const data = await updateMediaConstraints(member);
                    dbUser.push(data);
                }
            }
            else {
                dbUser = await updateMediaConstraints(req.body);
            }
        }
        catch (error) {
            res.status(200).send({ result: 'Error', message: error.message });
        }
        if (dbUser) {
            res.status(200).send({ result: 'OK', data: dbUser });
        }
        else {
            res.status(200).send({ result: 'Error', message: 'classroom are not fined!' });
        }
    });
    roomRouter.get('/list', async (req, res) => {
        const rooms = await MediaEntity_1.MediaConstraintEntity.find();
        res.status(200).send(rooms);
    });
    roomRouter.get('/delete/:id', async (req, res) => {
        const { id } = req.params;
        await MediaEntity_1.MediaConstraintEntity.delete({ id });
        res.status(200).send({ result: 'OK' });
    });
    roomRouter.get('/detail/:id', async (req, res) => {
        const { id } = req.params;
        let dbRoom = await MediaEntity_1.MediaConstraintEntity.findOne({ id });
        if (dbRoom) {
            res.status(200).send(dbRoom);
        }
        else {
            res.status(404).send(`Room ${id} do not existed!`);
        }
    });
    return roomRouter;
}
exports.mediaRouter = mediaRouter;
